package com.example.news_reader;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.util.Log;


public class DataParsing {

	protected ArrayList<DataList> data;
	DataList list;
	String u;
	String category=null;
	int cnt=0;

	public DataParsing(String url){
		// TODO Auto-generated constructor stub
		this.u = url;
		Log.i("url",url);
	}

	public ArrayList<DataList> getData(){
		data = new ArrayList<DataList>();
		
		try {

			URL text = new URL(u); // 파싱하고자하는 URL
			//Log.i("test",""+text);
			XmlPullParserFactory parserCreator = XmlPullParserFactory.newInstance(); 

			XmlPullParser parser = parserCreator.newPullParser(); // XMLPullParser 사용

			HttpURLConnection conn = (HttpURLConnection) text.openConnection();
			conn.setRequestProperty("accept-language","utf-8");

			Reader reader = new InputStreamReader(conn.getInputStream());
			BufferedReader buf = new BufferedReader(reader);

			parser.setInput(buf);   // 파싱하기위해서 스트림을 열어야한다. 
			Log.i("open","stream");
			int parserEvent = parser.getEventType();  // 파싱할 데이터의 타입을 알려준다.

			String tag;

			boolean inTitle = false, inTitle2 = false, inTitle3 = false, inTitle4 = false,inItem = false;
			data = new ArrayList<DataList>();
			int num = parserEvent;
			if (cnt==5) num=cnt;
			cnt=0;
			while (cnt != num) { // xml 파일의 문서 끝인가? 
				switch (parserEvent) {

					case XmlPullParser.TEXT:
						tag = parser.getName();
						if (inItem){
							if (inTitle) {
								if(category!=null){
									String a = "["+category+"]"+parser.getText();
									list.setName(a);
								}
								else
									list.setName(parser.getText());
							}else if (inTitle2) {
								list.setLink(parser.getText());
							}else if (inTitle3) {
								list.setSub(parser.getText());
							}else if (inTitle4) {
								list.setDate(parser.getText());
							}}

						break;

					case XmlPullParser.END_TAG: // 나중에
						tag = parser.getName();
						if (tag.compareTo("title") == 0) {  // 태그가 address인지 비교
							inTitle = false;
						} else if (tag.compareTo("link") == 0) { 
							inTitle2 = false;
						}  else if (tag.compareTo("description") == 0) { 
							inTitle3 = false;
						} else if (tag.compareTo("pubDate") == 0) { 
							inTitle4 = false;
						} else if (tag.compareTo("item") == 0) { 
							data.add(list);
							if(num==5)cnt++;
							inItem = false;
						} 

						break;

					case XmlPullParser.START_TAG: // 먼저 
						tag = parser.getName();
						if (tag.compareTo("item") == 0) {
							list = new DataList(null, null, null, null);
							inItem = true;
						} else if (tag.compareTo("title") == 0) {
							inTitle = true;
						} else if (tag.compareTo("link") == 0) {
							inTitle2 = true;
						} else if (tag.compareTo("description") == 0) {
							inTitle3 = true;
						} else if (tag.compareTo("pubDate") == 0) {
							inTitle4 = true;
						} 
						break;
				}
				parserEvent = parser.next();
				if(parserEvent==XmlPullParser.END_DOCUMENT)break;
			}
		} catch (Exception e) {
			Log.e("dd1",""+e.getMessage());
			Log.e("dd1", "error", e);
		}

		if(data.size()==0){
			data.add(new DataList("검색된 결과가 없습니다.", null, "", ""));
		}else{
		}

		return data;
	}
	
	public ArrayList<DataList> getAllData(){
		cnt = 5;
		return getData();
	}
	
	public ArrayList<DataList> getAllData(String cat){
		cnt = 5;
		category = cat;
		return getData();
	}

}
